﻿package morn.core.components
{
    import __AS3__.vec.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import morn.core.events.*;
    import morn.core.handlers.*;

    public class List extends Box implements IList, IItem
    {
        protected var _content:Box;
        protected var _scrollBar:ScrollBar;
        protected var _itemRender:Object;
        protected var _repeatX:int = 1;
        protected var _repeatY:int = 1;
        protected var _spaceX:int;
        protected var _spaceY:int;
        protected var _cells:Vector.<Box>;
        protected var _array:Array;
        protected var _startIndex:int;
        protected var _selectedIndex:int = -1;
        protected var _selectHandler:Handler;
        protected var _renderHandler:Handler;
        protected var _mouseHandler:Handler;
        protected var _page:int;
        protected var _totalPage:int;
        protected var _selectEnable:Boolean = true;
        protected var _isVerticalLayout:Boolean = true;
        protected var _cellSize:Number = 20;
        protected var _barShow:Boolean = true;
        protected var _inited:Boolean = false;

        public function List()
        {
            this._cells = new Vector.<Box>;
            this._array = [];
            return;
        }// end function

        override protected function createChildren() : void
        {
            var _loc_1:* = new Box();
            this._content = new Box();
            addChild(_loc_1);
            if (App.CurPluginName == null)
            {
                this._inited = true;
            }
            else
            {
                App.skinManager.addViewInitHandler(App.CurPluginName, this.initList);
            }
            return;
        }// end function

        public function initList() : void
        {
            if (!this._inited)
            {
                this._inited = true;
                callLater(this.changeCells);
                this.array = this._array;
            }
            return;
        }// end function

        override public function removeChild(param1:DisplayObject) : DisplayObject
        {
            return param1 != this._content ? (super.removeChild(param1)) : (null);
        }// end function

        override public function removeChildAt(param1:int) : DisplayObject
        {
            return getChildAt(param1) != this._content ? (super.removeChildAt(param1)) : (null);
        }// end function

        public function get content() : Box
        {
            return this._content;
        }// end function

        public function get scrollBar() : ScrollBar
        {
            return this._scrollBar;
        }// end function

        public function set scrollBar(param1:ScrollBar) : void
        {
            if (this._scrollBar != param1)
            {
                this._scrollBar = param1;
                if (this._scrollBar)
                {
                    addChild(this._scrollBar);
                    this._scrollBar.target = this;
                    this._scrollBar.addEventListener(Event.CHANGE, this.onScrollBarChange);
                    this._isVerticalLayout = this._scrollBar.direction == ScrollBar.VERTICAL;
                }
            }
            return;
        }// end function

        public function get itemRender()
        {
            return this._itemRender;
        }// end function

        public function set itemRender(param1) : void
        {
            this._itemRender = param1;
            callLater(this.changeCells);
            return;
        }// end function

        public function changeNow() : void
        {
            exeCallLater(this.changeCells);
            return;
        }// end function

        public function get repeatX() : int
        {
            return this._repeatX;
        }// end function

        public function set repeatX(param1:int) : void
        {
            this._repeatX = param1;
            callLater(this.changeCells);
            return;
        }// end function

        public function get repeatY() : int
        {
            return this._repeatY;
        }// end function

        public function set repeatY(param1:int) : void
        {
            this._repeatY = param1;
            callLater(this.changeCells);
            return;
        }// end function

        public function get spaceX() : int
        {
            return this._spaceX;
        }// end function

        public function set spaceX(param1:int) : void
        {
            this._spaceX = param1;
            callLater(this.changeCells);
            return;
        }// end function

        public function get spaceY() : int
        {
            return this._spaceY;
        }// end function

        public function set spaceY(param1:int) : void
        {
            this._spaceY = param1;
            callLater(this.changeCells);
            return;
        }// end function

        protected function changeCells() : void
        {
            var _loc_1:Box = null;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Number = NaN;
            var _loc_7:Number = NaN;
            if (this._itemRender)
            {
                for each (_loc_1 in this._cells)
                {
                    
                    _loc_1.removeEventListener(MouseEvent.CLICK, this.onCellMouse);
                    _loc_1.removeEventListener(MouseEvent.ROLL_OVER, this.onCellMouse);
                    _loc_1.removeEventListener(MouseEvent.ROLL_OUT, this.onCellMouse);
                    _loc_1.removeEventListener(MouseEvent.MOUSE_DOWN, this.onCellMouse);
                    _loc_1.removeEventListener(MouseEvent.MOUSE_UP, this.onCellMouse);
                    _loc_1.remove();
                    if (!(this._itemRender is XML || this._itemRender is Component))
                    {
                        App.renderItemFactory.add(this._itemRender, _loc_1);
                    }
                }
                this._cells.length = 0;
                this.scrollBar = getChildByName("scrollBar") as ScrollBar;
                _loc_2 = this._isVerticalLayout ? (this._repeatX) : (this._repeatY);
                _loc_3 = (this._isVerticalLayout ? (this._repeatY) : (this._repeatX)) + (this._scrollBar ? (1) : (0));
                _loc_4 = 0;
                while (_loc_4 < _loc_3)
                {
                    
                    _loc_5 = 0;
                    while (_loc_5 < _loc_2)
                    {
                        
                        if (this._itemRender is XML)
                        {
                            _loc_1 = this._itemRender is XML ? (View.createComp(this._itemRender) as Box) : (new this._itemRender());
                        }
                        else if (this._itemRender is Component)
                        {
                            _loc_1 = new this._itemRender();
                        }
                        else
                        {
                            _loc_1 = View.createCompObj(this._itemRender, null, null, true, true) as Box;
                        }
                        _loc_1.x = (this._isVerticalLayout ? (_loc_5) : (_loc_4)) * (this._spaceX + _loc_1.width);
                        _loc_1.y = (this._isVerticalLayout ? (_loc_4) : (_loc_5)) * (this._spaceY + _loc_1.height);
                        this._content.addChild(_loc_1);
                        this.addCell(_loc_1);
                        _loc_5++;
                    }
                    _loc_4++;
                }
                if (this._scrollBar)
                {
                    _loc_6 = _loc_1.width + this.spaceX;
                    _loc_7 = _loc_1.height + this.spaceY;
                    this._cellSize = this._isVerticalLayout ? (_loc_7) : (_loc_6);
                    this.setContentSize(_loc_6 * this._repeatX, _loc_7 * this._repeatY);
                }
            }
            return;
        }// end function

        protected function addCell(param1:Box) : void
        {
            param1.addEventListener(MouseEvent.CLICK, this.onCellMouse);
            param1.addEventListener(MouseEvent.ROLL_OVER, this.onCellMouse);
            param1.addEventListener(MouseEvent.ROLL_OUT, this.onCellMouse);
            param1.addEventListener(MouseEvent.MOUSE_DOWN, this.onCellMouse);
            param1.addEventListener(MouseEvent.MOUSE_UP, this.onCellMouse);
            this._cells.push(param1);
            return;
        }// end function

        public function initItems() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Box = null;
            if (!this._itemRender)
            {
                _loc_1 = 0;
                while (_loc_1 < int.MAX_VALUE)
                {
                    
                    _loc_2 = getChildByName("item" + _loc_1) as Box;
                    if (_loc_2)
                    {
                        this.addCell(_loc_2);
                    }
                    else
                    {
                        break;
                    }
                    _loc_1++;
                }
            }
            return;
        }// end function

        public function setContentSize(param1:Number, param2:Number) : void
        {
            var _loc_3:* = this._content.graphics;
            _loc_3.clear();
            _loc_3.beginFill(16776960, 0);
            _loc_3.drawRect(0, 0, param1, param2);
            _loc_3.endFill();
            this._content.width = param1;
            this._content.height = param2;
            this._content.scrollRect = new Rectangle(0, 0, param1, param2);
            return;
        }// end function

        protected function onCellMouse(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as Box;
            var _loc_3:* = this._startIndex + this._cells.indexOf(_loc_2);
            if (event.type == MouseEvent.ROLL_OVER || event.type == MouseEvent.ROLL_OUT || event.type == MouseEvent.MOUSE_DOWN)
            {
                if (event.type == MouseEvent.MOUSE_DOWN)
                {
                    if (this._selectEnable)
                    {
                        this.selectedIndex = _loc_3;
                    }
                    else
                    {
                        this.changeCellState(_loc_2, true, 0);
                    }
                }
                else if (this._selectedIndex != _loc_3)
                {
                    this.changeCellState(_loc_2, event.type == MouseEvent.ROLL_OVER, 0);
                }
            }
            if (this._mouseHandler != null)
            {
                this._mouseHandler.executeWith([event, _loc_3]);
            }
            return;
        }// end function

        protected function changeCellState(param1:Box, param2:Boolean, param3:int) : void
        {
            var _loc_4:* = param1.getChildByName("selectBox") as Clip;
            if (param1.getChildByName("selectBox") as Clip)
            {
                _loc_4.visible = param2;
                _loc_4.frame = param3;
            }
            return;
        }// end function

        protected function onScrollBarChange(event:Event) : void
        {
            var _loc_2:* = this._content.scrollRect;
            var _loc_3:* = this._scrollBar.value;
            var _loc_4:* = int(_loc_3 / this._cellSize) * (this._isVerticalLayout ? (this._repeatX) : (this._repeatY));
            if (int(_loc_3 / this._cellSize) * (this._isVerticalLayout ? (this._repeatX) : (this._repeatY)) != this._startIndex)
            {
                this.startIndex = _loc_4;
            }
            if (this._isVerticalLayout)
            {
                _loc_2.y = _loc_3 % this._cellSize;
            }
            else
            {
                _loc_2.x = _loc_3 % this._cellSize;
            }
            this._content.scrollRect = _loc_2;
            return;
        }// end function

        public function get selectEnable() : Boolean
        {
            return this._selectEnable;
        }// end function

        public function set selectEnable(param1:Boolean) : void
        {
            this._selectEnable = param1;
            return;
        }// end function

        public function get selectedIndex() : int
        {
            return this._selectedIndex;
        }// end function

        public function set selectedIndex(param1:int) : void
        {
            if (this._selectedIndex != param1)
            {
                this._selectedIndex = param1;
                this.changeSelectStatus();
                if (this._selectHandler != null)
                {
                    this._selectHandler.executeWith([param1]);
                }
                sendEvent(Event.SELECT);
            }
            return;
        }// end function

        protected function changeSelectStatus() : void
        {
            var _loc_1:int = 0;
            var _loc_2:* = this._cells.length;
            while (_loc_1 < _loc_2)
            {
                
                this.changeCellState(this._cells[_loc_1], this._selectedIndex == this._startIndex + _loc_1, 1);
                _loc_1++;
            }
            return;
        }// end function

        public function get selectedItem() : Object
        {
            return this._selectedIndex != -1 ? (this._array[this._selectedIndex]) : (null);
        }// end function

        public function set selectedItem(param1:Object) : void
        {
            this.selectedIndex = this._array.indexOf(param1);
            return;
        }// end function

        public function get selection() : Box
        {
            return this.getCell(this._selectedIndex);
        }// end function

        public function set selection(param1:Box) : void
        {
            this.selectedIndex = this._startIndex + this._cells.indexOf(param1);
            return;
        }// end function

        public function get selectHandler() : Handler
        {
            return this._selectHandler;
        }// end function

        public function set selectHandler(param1:Handler) : void
        {
            this._selectHandler = param1;
            return;
        }// end function

        public function get renderHandler() : Handler
        {
            return this._renderHandler;
        }// end function

        public function set renderHandler(param1:Handler) : void
        {
            this._renderHandler = param1;
            return;
        }// end function

        public function get mouseHandler() : Handler
        {
            return this._mouseHandler;
        }// end function

        public function set mouseHandler(param1:Handler) : void
        {
            this._mouseHandler = param1;
            return;
        }// end function

        public function get startIndex() : int
        {
            return this._startIndex;
        }// end function

        public function set startIndex(param1:int) : void
        {
            this._startIndex = param1 > 0 ? (param1) : (0);
            callLater(this.renderItems);
            return;
        }// end function

        protected function renderItems() : void
        {
            var _loc_1:int = 0;
            var _loc_2:* = this._cells.length;
            while (_loc_1 < _loc_2)
            {
                
                this.renderItem(this._cells[_loc_1], this._startIndex + _loc_1);
                _loc_1++;
            }
            this.changeSelectStatus();
            return;
        }// end function

        protected function renderItem(param1:Box, param2:int) : void
        {
            if (param2 < this._array.length)
            {
                param1.visible = true;
                param1.dataSource = this._array[param2];
            }
            else
            {
                param1.visible = false;
            }
            if (this._renderHandler != null)
            {
                this._renderHandler.executeWith([param1, param2]);
            }
            sendEvent(UIEvent.ITEM_RENDER, [param1, param2]);
            return;
        }// end function

        public function get array() : Array
        {
            return this._array;
        }// end function

        public function set array(param1:Array) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            exeCallLater(this.changeCells);
            this._array = param1 || [];
            var _loc_2:* = this._array.length;
            this._totalPage = Math.ceil(_loc_2 / (this._repeatX * this._repeatY));
            this._selectedIndex = this._selectedIndex < _loc_2 ? (this._selectedIndex) : ((_loc_2 - 1));
            this.startIndex = this._startIndex;
            if (this._scrollBar)
            {
                _loc_3 = this._isVerticalLayout ? (this._repeatY) : (this._repeatX);
                _loc_4 = this._isVerticalLayout ? (this._repeatX) : (this._repeatY);
                _loc_5 = Math.ceil(_loc_2 / (this._isVerticalLayout ? (this._repeatX) : (this._repeatY)));
                this._scrollBar.visible = this._barShow ? (true) : (this._totalPage > 1);
                if (this._totalPage > 1)
                {
                    this._scrollBar.scrollSize = this._cellSize;
                    this._scrollBar.thumbPercent = _loc_3 / _loc_5;
                    this._scrollBar.setScroll(0, (_loc_5 - _loc_3) * this._cellSize, this._startIndex / _loc_4 * this._cellSize);
                }
                else
                {
                    this._scrollBar.setScroll(0, 0, 0);
                }
            }
            return;
        }// end function

        public function get totalPage() : int
        {
            return this._totalPage;
        }// end function

        public function set totalPage(param1:int) : void
        {
            this._totalPage = param1;
            return;
        }// end function

        public function get page() : int
        {
            return this._page;
        }// end function

        public function set page(param1:int) : void
        {
            this._page = param1 > 0 ? (param1) : (0);
            this._page = this._page < this._totalPage ? (this._page) : (this._totalPage > 0 ? ((this._totalPage - 1)) : (0));
            this.startIndex = this._page * this._repeatX * this._repeatY;
            return;
        }// end function

        public function get length() : int
        {
            return this._array.length;
        }// end function

        override public function set dataSource(param1:Object) : void
        {
            _dataSource = param1;
            if (param1 is Array)
            {
                this.array = param1 as Array;
            }
            else
            {
                super.dataSource = param1;
            }
            return;
        }// end function

        public function get cells() : Vector.<Box>
        {
            exeCallLater(this.changeCells);
            return this._cells;
        }// end function

        override public function commitMeasure() : void
        {
            exeCallLater(this.changeCells);
            return;
        }// end function

        public function refresh() : void
        {
            this.array = this._array;
            return;
        }// end function

        public function getItem(param1:int) : Object
        {
            if (param1 > -1 && param1 < this._array.length)
            {
                return this._array[param1];
            }
            return null;
        }// end function

        public function changeItem(param1:int, param2:Object) : void
        {
            if (param1 > -1 && param1 < this._array.length)
            {
                this._array[param1] = param2;
                if (param1 >= this._startIndex && param1 < this._startIndex + this._cells.length)
                {
                    this.renderItem(this.getCell(param1), param1);
                }
            }
            return;
        }// end function

        public function addItem(param1:Object) : void
        {
            this._array.push(param1);
            this.array = this._array;
            return;
        }// end function

        public function addItemAt(param1:Object, param2:int) : void
        {
            this._array.splice(param2, 0, param1);
            this.array = this._array;
            return;
        }// end function

        public function deleteItem(param1:int) : void
        {
            this._array.splice(param1, 1);
            this.array = this._array;
            return;
        }// end function

        public function getCell(param1:int) : Box
        {
            exeCallLater(this.changeCells);
            if (param1 > -1 && this._cells)
            {
                return this._cells[(param1 - this._startIndex) % this._cells.length];
            }
            return null;
        }// end function

        public function scrollTo(param1:int) : void
        {
            this.startIndex = param1;
            if (this._scrollBar)
            {
                this._scrollBar.value = param1 * this._cellSize;
            }
            return;
        }// end function

        public function set barShow(param1:Boolean) : void
        {
            this._barShow = param1;
            return;
        }// end function

    }
}
